<template>
  <div>
    <div class="swiper-thumbs-container">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="(item, index) in tabs"
          :key="index"
          :class="[
            'us-scroll-line-col',
            { 'us-scroll-active-line-col': modelValue === index },
          ]"
        >
          <label class="us-nav-tit" :class="{ active: modelValue === index }">{{
            item.title
          }}</label>
        </div>
      </div>
    </div>
    <div class="swiper-cards-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="item in tabs" :key="item.type">
          <slot :name="item.type"></slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper/js/swiper.js';
import 'swiper/css/swiper.css';

export default {
  name: 'LinkageSwiper',
  data() {
    return {
      myThumbSwiper: null,
      myCardSwiper: null,
    };
  },
  props: {
    modelValue: {
      type: [Number, String],
    },
    tabs: {
      type: Array,
      default() {
        return [];
      },
    },
    slideClass: String,
    slideActiveClass: String,
  },
  methods: {
    initSwiper() {
      const _this = this;
      this.myThumbSwiper = new Swiper('.swiper-thumbs-container', {
        initialSlide: this.modelValue,
        slidesPerView: 'auto',
        centeredSlides: true,
        centeredSlidesBounds: true,
        freeMode: true,
        // resizeObserver: false,
        // updateOnWindowResize: false,
        on: {
          tap: () => {
            if (this.myThumbSwiper.clickedIndex === undefined) {
              return false;
            }
            _this.$emit('update:modelValue', this.myThumbSwiper.clickedIndex);
          },
        },
      });

      this.myCardSwiper = new Swiper('.swiper-cards-container', {
        watchOverflow: true,
        centeredSlides: true,
        thumbs: {
          swiper: this.myThumbSwiper,
        },
        on: {
          slideChangeTransitionEnd: function () {
            const index = this.realIndex;
            _this.$emit('update:modelValue', index);
          },
        },
      });
    },
  },
  mounted() {
    this.initSwiper();
    window.addEventListener('resize', () => {
      this.$nextTick(() => {
        this.myThumbSwiper.$el[0].scrollTo({
          left: -200,
        });
      });
    });
  },
  watch: {
    modelValue: {
      immediate: true,
      handler(val) {
        this.$nextTick(() => {
          this.myThumbSwiper && this.myThumbSwiper.slideTo(val);
        });
      },
    },
  },
  components: {},
};
</script>
<style lang="scss">
.swiper-thumbs-container {
  height: 55px;
  background: white;
  overflow-x: hidden;
  overflow-y: auto;

  .swiper-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 1;
    display: flex;
    transition-property: transform;
    box-sizing: content-box;
  }
  .us-scroll-line-col:first-of-type {
    margin-left: 20px;
  }
  .us-scroll-line-col {
    position: relative;
    height: 44px !important;
    text-align: center;
    width: auto !important;
    flex: 1 0 auto;
    margin: 0 12px;
    .us-nav-tit {
      line-height: 44px;
      font-size: 18px;
    }
    .us-nav-tit.active {
      color: #f84848;
      font-size: 18px;
      font-weight: 600;
    }
    .us-nav-tit.active:before {
      position: absolute;
      left: 50%;
      bottom: 0;
      content: "";
      z-index: 2;
      width: 20px;
      height: 5px;
      margin-left: -10px;
      background: url()
        no-repeat center center;
      background-size: contain;
    }
  }
}

.swiper-cards-container {
  overflow-x: hidden;
  overflow-y: auto;
  position: relative;
  height: calc(100vh - 100px);
}
</style>
